#include "feature.hpp"
#include "orb.hpp"

#include <boost/filesystem.hpp>
#include <glog/logging.h>
#include <gflags/gflags.h>
#include <opencv2/opencv.hpp>

DEFINE_string(image, "", "待处理的文件名");

namespace fs = boost::filesystem;

int main(int argc, char* argv[])
{
    google::InitGoogleLogging(argv[0]);
	google::SetLogDestination(google::GLOG_INFO, "./Log/INFO_");
    google::SetLogDestination(google::GLOG_WARNING, "./Log/WARNING_");
	google::SetLogDestination(google::GLOG_ERROR, "./Log/ERROR_");
	google::SetLogDestination(google::GLOG_FATAL, "./Log/FATAL_");
	google::SetStderrLogging(google::GLOG_ERROR);

    fs::path absLogPath = fs::system_complete("./Log");
    if (! fs::exists(absLogPath)) {
        fs::create_directories(absLogPath);
    }
    std::cout << "absLogPath: " << absLogPath << std::endl;

    gflags::ParseCommandLineFlags(&argc, &argv, true);
    LOG(INFO) << "image file: " << FLAGS_image;

    fs::path absImagePath = fs::system_complete(FLAGS_image);
    if(! fs::exists(absImagePath)) {
        LOG(ERROR) << "image file not exists: " << absImagePath;
        return -1;
    }
    LOG(INFO) << "absImagePath: " << absImagePath;

    cv::Mat testImage = cv::imread(absImagePath.c_str(), cv::IMREAD_GRAYSCALE);
    if (testImage.data == NULL) {
        LOG(ERROR) << "cv::imread failed";
        return -1;
    }

    // cv::imshow("testImage", testImage);
    // cv::waitKey(0);

    slam::OrbAttr orbAttr;
    orbAttr.edgeThreshold = 16;
    orbAttr.fastThreshold = 0.2;
    orbAttr.firstLevel = 0;
    orbAttr.nfeatures = 500;
    orbAttr.nlevels = 8;
    orbAttr.patchSize = 16;
    orbAttr.scaleFactor = 1.1f;
    orbAttr.scoreType = cv::ORB::HARRIS_SCORE;
    orbAttr.wta_k = 2;
    slam::Feature<slam::ORB, slam::OrbAttr> instance;
    instance.Init(orbAttr);
    instance.Extract(testImage);
    // instance.Clean();
    instance.DrawKeyPoints();
    cv::waitKey(0);

    return 0;
}